/**
 * Copyright 2024 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * khi-file-types.ts
 * Defines the schema of the JSON structure stored at the beginning of KHI file structure.
 */

import {
  LogType,
  ParentRelationship,
  RevisionState,
  RevisionVerb,
  Severity,
} from 'src/app/generated';
import { InspectionMetadataHeader } from './metadata-types';

export interface KHIFile {
  version?: string;
  metadata: KHIFileMetadata;
  resources: KHIFileResource[];
  logs: KHIFileLog[];
  timelines: KHIFileTimeline[];
}

export const LogAnnotationTypeResourceRef = 'resource_ref';

export interface KHILogAnnotation {
  type: string;
  [key: string]: unknown;
}

export interface KHIFileTextReference {
  /**
   * Offset from the beginning of the text buffer
   */
  offset: number;
  /**
   * Length of the string
   */
  len: number;
  /**
   * Index of the buffer
   */
  buffer: number;
}

export interface KHIFileLog {
  /**
   * Log ID
   */
  id: string;
  /**
   * Log ID visible to user. This ID can be duplicated with another ID.
   */
  displayId: string;
  /**
   * Timestamp
   */
  ts: string;
  /**
   * Severity of the log
   */
  severity: Severity;
  /**
   * Body of this log
   */
  body: KHIFileTextReference;
  /**
   * Type of the processor parsing this log
   */
  type: LogType;
  /**
   * Summary of the log generated by KHI.
   */
  summary: KHIFileTextReference;

  annotations: KHILogAnnotation[];
}

export interface KHIFileResourceEvent {
  /**
   * log ID related to this event.
   */
  log: string;
}

export interface KHIFileResourceRevision {
  /**
   * Log ID related to this revision.
   */
  log: string;
  /**
   * Verb of the request made this change.
   */
  verb: RevisionVerb;
  /**
   * Body of the manifest at this revision.
   */
  body: KHIFileTextReference;
  /**
   * Requestor of the modification
   */
  requestor: KHIFileTextReference;
  /**
   * If this manifest is partial, otherwise this manifest should contain full body of the manifest.
   */
  partial: boolean;

  changeTime: string;

  /**
   * The current state of the resource
   */
  state: RevisionState;
}

export interface KHIFileResource {
  /**
   * Name of this resource.
   */
  name: string;
  /**
   * Timeline ID bound to this resource.
   */
  timeline: string;
  /**
   * Relationship between this resource and parent resource.
   */
  relationship: ParentRelationship;
  /**
   * Children of this resource.
   */
  children: KHIFileResource[];

  /**
   * The full path of the resource
   */
  path: string;
}

export type KHIFileMetadata = {
  header: InspectionMetadataHeader;
};

export interface KHIFileTimeline {
  /**
   * Timeline ID
   */
  id: string;
  /**
   * List of revisions associated to this timeline.
   */
  revisions: KHIFileResourceRevision[];
  /**
   * List of events associated to this timeline.
   */
  events: KHIFileResourceEvent[];
}
